/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.biz.modular.cooperation.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import vip.xiaonuo.common.annotation.CommonLog;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.biz.modular.cooperation.entity.BizCooperation;
import vip.xiaonuo.biz.modular.cooperation.param.BizCooperationAddParam;
import vip.xiaonuo.biz.modular.cooperation.param.BizCooperationEditParam;
import vip.xiaonuo.biz.modular.cooperation.param.BizCooperationIdParam;
import vip.xiaonuo.biz.modular.cooperation.param.BizCooperationPageParam;
import vip.xiaonuo.biz.modular.cooperation.service.BizCooperationService;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * 互助金成员信息记录表控制器
 *
 * @author 薛守立
 * @date  2024/09/08 14:54
 */
@Tag(name = "互助金成员信息记录表控制器")
@RestController
@Validated
public class BizCooperationController {

    @Resource
    private BizCooperationService bizCooperationService;

    /**
     * 获取互助金成员信息记录表分页
     *
     * @author 薛守立
     * @date  2024/09/08 14:54
     */
    @Operation(summary = "获取互助金成员信息记录表分页")
    @SaCheckPermission("/biz/cooperation/page")
    @GetMapping("/biz/cooperation/page")
    public CommonResult<Page<BizCooperation>> page(BizCooperationPageParam bizCooperationPageParam) {
        return CommonResult.data(bizCooperationService.page(bizCooperationPageParam, bizCooperationPageParam.getApplyType()));
    }

    /**
     * 添加互助金成员信息记录表
     *
     * @author 薛守立
     * @date  2024/09/08 14:54
     */
    @Operation(summary = "添加互助金成员信息记录表")
    @CommonLog("添加互助金成员信息记录表")
    @SaCheckPermission("/biz/cooperation/add")
    @PostMapping("/biz/cooperation/add")
    public CommonResult<String> add(@RequestBody @Valid BizCooperationAddParam bizCooperationAddParam) {
        bizCooperationService.add(bizCooperationAddParam);
        return CommonResult.ok();
    }

    /**
     * 编辑互助金成员信息记录表
     *
     * @author 薛守立
     * @date  2024/09/08 14:54
     */
    @Operation(summary = "编辑互助金成员信息记录表")
    @CommonLog("编辑互助金成员信息记录表")
    @SaCheckPermission("/biz/cooperation/edit")
    @PostMapping("/biz/cooperation/edit")
    public CommonResult<String> edit(@RequestBody @Valid BizCooperationEditParam bizCooperationEditParam) {
        bizCooperationService.edit(bizCooperationEditParam);
        return CommonResult.ok();
    }

    /**
     * 编辑互助金成员信息记录表
     *
     * @author 薛守立
     * @date  2024/09/08 14:54
     */
    @Operation(summary = "审批成员的互助金申请状态")
    @CommonLog("审批成员的互助金申请状态")
    @SaCheckPermission("/biz/cooperation/apply")
    @PostMapping("/biz/cooperation/apply")
    public CommonResult<String> apply(@RequestBody @Valid BizCooperationEditParam bizCooperationEditParam) {
        bizCooperationService.apply(bizCooperationEditParam);
        return CommonResult.ok();
    }

    /**
     * 删除互助金成员信息记录表
     *
     * @author 薛守立
     * @date  2024/09/08 14:54
     */
    @Operation(summary = "删除互助金成员信息记录表")
    @CommonLog("删除互助金成员信息记录表")
    @SaCheckPermission("/biz/cooperation/delete")
    @PostMapping("/biz/cooperation/delete")
    public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
                                       List<BizCooperationIdParam> bizCooperationIdParamList) {
        bizCooperationService.delete(bizCooperationIdParamList);
        return CommonResult.ok();
    }

    /**
     * 获取互助金成员信息记录表详情
     *
     * @author 薛守立
     * @date  2024/09/08 14:54
     */
    @Operation(summary = "获取互助金成员信息记录表详情")
    @SaCheckPermission("/biz/cooperation/detail")
    @GetMapping("/biz/cooperation/detail")
    public CommonResult<BizCooperation> detail(@Valid BizCooperationIdParam bizCooperationIdParam) {
        return CommonResult.data(bizCooperationService.detail(bizCooperationIdParam));
    }


    /**
     * 导入互助金的台账信息
     *
     * @author 薛守立
     * @date  2024/09/08 14:54
     */
    @Operation(summary = "导入互助金成员的台账信息")
    @SaCheckPermission("/biz/cooperation/exportIn")
    @GetMapping("/biz/cooperation/exportIn")
    public CommonResult<String> exportIn(@RequestPart("file") MultipartFile file) {
        return CommonResult.data(bizCooperationService.exportIn(file));
    }

    /**
     * 导出互助金成员的台账信息
     *
     * @author 薛守立
     * @date  2024/09/08 14:54
     */
    @Operation(summary = "导出互助金成员的台账信息")
    @SaCheckPermission("/biz/cooperation/export")
    @GetMapping("/biz/cooperation/export")
    public void exportOut(HttpServletResponse httpServletResponse) throws IOException {
        httpServletResponse.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        httpServletResponse.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("互助金成员成员台账", StandardCharsets.UTF_8).replaceAll("\\+", "%20");
        List<BizCooperation> cooperationList = bizCooperationService.getAllCooperation();
        httpServletResponse.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(httpServletResponse.getOutputStream(), BizCooperation.class)
                .sheet("sheet")
                .doWrite(cooperationList);
    }

    /**
     * 获取互助金成员信息记录表分页
     *
     * @author 薛守立
     * @date  2024/09/08 14:54
     */
    @Operation(summary = "获取互助金成员信息记录表分页")
    @SaCheckPermission("/biz/cooperation/pageApply")
    @GetMapping("/biz/cooperation/pageApply")
    public CommonResult<Page<BizCooperation>> pageApply(BizCooperationPageParam bizCooperationPageParam) throws IOException {
        return CommonResult.data(bizCooperationService.page(bizCooperationPageParam, "30"));
    }

}
